package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.MD5Utils;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.Tool;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 利息补贴
 * BT_ITEM_RATE_SUBSIDY 	===> nd_interest_subsidy_table
 */
@BusinessDataConverter(
        insertSql = "INSERT INTO ND_INTEREST_SUBSIDY_TABLE(ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, PAYMENT_NAME, PAYMENT_ACCOUNT, DEPOSIT_BANK, SUBSIDY_AMOUNT, SUBSIDY_RATE, CLAIM_MARK) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE ND_INTEREST_SUBSIDY_TABLE SET CREATE_NAME = ?, CREATE_BY = ?, CREATE_DATE = ?, UPDATE_NAME = ?, UPDATE_BY = ?, UPDATE_DATE = ?, SYS_ORG_CODE = ?, SYS_COMPANY_CODE = ?, BPM_STATUS = ?, PRO_ID = ?, PAYMENT_NAME = ?, PAYMENT_ACCOUNT = ?, DEPOSIT_BANK = ?, SUBSIDY_AMOUNT = ?, SUBSIDY_RATE = ?, CLAIM_MARK = ? WHERE ID = ?",
        findMigratedIdListSql = " select ID from nd_interest_subsidy_table where PRO_ID = ?",
        seq = 211
)
public class NdInterestSubsidyTable extends AbstractBusiness {

    public NdInterestSubsidyTable(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws Exception {

        List<Map> btItemRateSubsidyList = this.sourceData.getBtItemRateSubsidy();

        for (Map<String,Object> btItemRateSubsidy : btItemRateSubsidyList) {
            // 判断是否需要覆盖迁移
            int needMigratedResult = this.needMigrated(buildId(btItemRateSubsidy));
            if(needMigratedResult == 0) {
                continue;
            }

            // 单条插入或更新
            if(needMigratedResult == 1) {
                // 批量插入或更新 和 单条插入或更新 二选一
                this.targetDbUtil.updateByParams(this.updateSql, this.buildValues(btItemRateSubsidy),true);
//                this.batchUpdateValues.add(this.buildValues(btItemRateSubsidy));
            } else {
                this.targetDbUtil.updateByParams(this.insertSql, this.buildValues(btItemRateSubsidy));
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchInsertValues.add(this.buildValues(btItemRateSubsidy));
            }
        }


    }

    public List buildValues(Map btItemRateSubsidy) throws Exception{

        List values = new ArrayList();


        //ID
        values.add(buildId(btItemRateSubsidy));
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btItemRateSubsidy.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btItemRateSubsidy.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);

        //PRO_ID	项目ID
        values.add(this.projectId);
        //PAYMENT_NAME	支付户名
        values.add(btItemRateSubsidy.get("PAYNAME"));
        //PAYMENT_ACCOUNT	支付账号
        values.add(btItemRateSubsidy.get("PAYACCOUNT"));
        //DEPOSIT_BANK	开户银行及支行
        values.add(btItemRateSubsidy.get("PAYBANK"));
        //SUBSIDY_AMOUNT	补贴金额
        values.add(btItemRateSubsidy.get("ASKSUM"));
        //SUBSIDY_RATE	补贴年利率
        values.add(btItemRateSubsidy.get("PAYRATE"));
        //CLAIM_MARK	申请利息标记 todo
        values.add(null);


        return values;
    }

    public String buildId(Map btItemRateSubsidy){
        // 基础表数据业务主键
        String originalId = btItemRateSubsidy.get("ITEMNO").toString()
                + "-" + btItemRateSubsidy.get("CONNACCOUNTLIST").toString()
                + "-" + btItemRateSubsidy.get("WORKFLOWNO").toString();
        return MD5Utils.stringToMD5(originalId);
    }
}
